/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.common.core.auth.client.interceptor;

import cn.hutool.core.util.StrUtil;
import com.medusa.gruul.common.core.auth.model.constant.Headers;
import com.medusa.gruul.common.core.constant.CommonConstants;
import com.medusa.gruul.common.core.auth.model.enums.AuthPlatform;
import com.medusa.gruul.common.core.auth.model.enums.Platform;
import com.medusa.gruul.common.core.auth.client.helper.ISecurity;
import com.medusa.gruul.common.core.util.CurUserUtil;
import com.medusa.gruul.common.dto.CurUserDto;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * feign携带客户端请求头
 * @author 张治保
 * date 2021/12/3
 */
@Slf4j
@Component
public class AuthFeignRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate request) {
        /*
         * tenantId
         */
        String tenantId = ISecurity.tenantId().can();
        if (StrUtil.isNotBlank(tenantId)) {
            request.header(CommonConstants.TENANT_ID, tenantId);
        }
        /*
         * shopId
         */
        String shopId = ISecurity.shopId().can();
        if (StrUtil.isNotBlank(shopId)) {
            request.header(CommonConstants.SHOP_ID, shopId);
        }
        /*
         * ip
         */
        String ip = ISecurity.ip().can();
        if (StrUtil.isNotBlank(ip)){
            request.header(Headers.IP,ip);
        }
        /*
         * version
         */
        String version = ISecurity.version().can();
        if (StrUtil.isNotBlank(version)){
            request.header(Headers.VERSION,version);
        }
        /*
         * platform
         */
        Platform platform = ISecurity.platform().can();
        if (platform != null){
            request.header(Headers.PLATFORM, platform.name());
        }
        /*
         * auth platform
         */
        AuthPlatform authPlatform = ISecurity.authPlatform().can();
        if (authPlatform != null){
            request.header(Headers.AUTH_PLATFORM, authPlatform.name());
        }
        /*
         * token
         */
        String token = ISecurity.token().can();
        if (StrUtil.isNotBlank(token)){
            request.header(Headers.TOKEN,token);
        }
        /*
         * userStr
         */
        String userStr = ISecurity.userStr().can();
        if (StrUtil.isNotBlank(userStr)){
            request.header(Headers.USER,userStr);
        }
        /*
         * user
         */
        CurUserDto dto= CurUserUtil.getHttpCurUser();
        String userId;
        if (dto!=null &&StrUtil.isNotBlank(userId =dto.getUserId()) ){
            request.header(CommonConstants.CUR_USER, userId);
        }
    }
}
